from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_tarifDetEdit

class TarifDetEdit(QDialog, Ui_tarifDetEdit.Ui_tarifDetEditDlg):
    
  StyleSheet = """
QLineEdit[mandatory="true"] {
    background-color: rgb(255, 255, 127);
    color: darkblue;
}
"""

  def __init__(self, dbConn, tipe,  dataGlob, parent=None):
        super(TarifDetEdit, self).__init__(parent)
        self.setupUi(self)
        self.db = dbConn
        self.tipe = tipe
        self.dataGlob = dataGlob
        
        self.mastModel = QSqlTableModel(self)
        self.mastModel.setTable("master_tarif_detail")
        self.mastModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
        
        self.lineedits = (self.id_le, self.desc_le)
        for lineEdit in self.lineedits:
            lineEdit.setProperty("mandatory", QVariant(True))
        self.setStyleSheet(TarifDetEdit.StyleSheet)
        
        self.tipeModel = QSqlTableModel(self)
        self.tipeModel.setTable("master_tarif")
        self.tipeModel.select()
        self.cat_cmb.setModel(self.tipeModel)
        self.cat_cmb.setModelColumn(1)
        self.cat_cmb.setCurrentIndex(4)
        
        self.initData()
        
#        untuk ambil data dari cmb
#        brs = self.tenantType_cmb.currentIndex()
#        print self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
#        print self.tipeModel.data(self.tipeModel.index(brs, 1)).toString()
        
  @pyqtSignature("")
  def on_save_pb_clicked(self):
      if self.tipe == "add":
          if self.tambahData():
              self.close()
          else:
              QMessageBox.warning(self, "Tarif Detail","Can not add new data.")
      else:
          if self.updateData():
              self.close()
          else:
              QMessageBox.warning(self, "Tarif Detail","Can not update data")
      
  @pyqtSignature("")
  def on_cancel_pb_clicked(self):
      self.close()

  def tambahData(self):
      if not self.validasi():
          return
      self.mastModel.select()      
      QSqlDatabase.database().transaction()
      if not self.mastModel.insertRow(0):
          return
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.id_le.text()))
      brs = self.cat_cmb.currentIndex()
      cat = self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(cat))            
      self.mastModel.setData(self.mastModel.index(0, 2), QVariant(self.desc_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 3), QVariant(self.type_cmb.currentText().left(1)))
      self.mastModel.setData(self.mastModel.index(0, 4), QVariant(self.usage_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 5), QVariant(self.abonemen_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 6), QVariant(self.ppju_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 7), QVariant(self.loss_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 8), QVariant(self.admin_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 9), QVariant(self.cap_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 10), QVariant(self.other_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 11), QVariant(self.ct_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 12), QVariant(self.range1_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 13), QVariant(self.prog2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 14), QVariant(self.range2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 15), QVariant(self.prog3_le.text()))
      
      self.mastModel.setData(self.mastModel.index(0, 17), QVariant(self.trafo_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 18), QVariant(self.maint_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 19), QVariant(self.adminMax_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 20), QVariant(self.stat_cmb.currentText().left(1)))
      self.mastModel.setData(self.mastModel.index(0, 21), QVariant(self.note_le.text()))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def updateData(self):
      if not self.validasi():
          return
      self.mastModel.setFilter("kode_tarif = '"+self.tipe+"'")
      self.mastModel.select()
      QSqlDatabase.database().transaction()
      self.mastModel.setData(self.mastModel.index(0, 0), QVariant(self.id_le.text()))
      brs = self.cat_cmb.currentIndex()
      cat = self.tipeModel.data(self.tipeModel.index(brs, 0)).toString()
      self.mastModel.setData(self.mastModel.index(0, 1), QVariant(cat))            
      self.mastModel.setData(self.mastModel.index(0, 2), QVariant(self.desc_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 3), QVariant(self.type_cmb.currentText().left(1)))
      self.mastModel.setData(self.mastModel.index(0, 4), QVariant(self.usage_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 5), QVariant(self.abonemen_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 6), QVariant(self.ppju_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 7), QVariant(self.loss_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 8), QVariant(self.admin_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 9), QVariant(self.cap_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 10), QVariant(self.other_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 11), QVariant(self.ct_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 12), QVariant(self.range1_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 13), QVariant(self.prog2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 14), QVariant(self.range2_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 15), QVariant(self.prog3_le.text()))
      
      self.mastModel.setData(self.mastModel.index(0, 17), QVariant(self.trafo_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 18), QVariant(self.maint_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 19), QVariant(self.adminMax_le.text()))
      self.mastModel.setData(self.mastModel.index(0, 20), QVariant(self.stat_cmb.currentText().left(1)))
      self.mastModel.setData(self.mastModel.index(0, 21), QVariant(self.note_le.text()))
      if not self.mastModel.submitAll():
          return
      if not QSqlDatabase.database().commit():
          return
      return True

  def initData(self):
      if self.tipe != "add":
          query = QSqlQuery(self.db)
          qStr = "select m.kode_tarif, m.kode_jenis_tarif, m.keterangan, m.tipe, m.tarif_satuan, m.tarif_beban, m.tarif_retribusi, m.tarif_ttlb, m.tarif_administrasi, m.tarif_daya, m.other, m.rasio, m.range1, m.tarif2, m.range2, m.tarif3, m.prog, m.trafo, m.tarif_pemel, m.max_adm_rp, m.status, m.ket from master_tarif_detail m where m.kode_tarif = '"+self.tipe+"'"
          query.exec_(qStr)
          query.next()
          self.id_le.setText(unicode(query.value(0).toString()))
          brs = self.tipeModel.match(self.tipeModel.index(0, 0), 0, query.value(1),1, Qt.MatchWrap )
          self.cat_cmb.setCurrentIndex(brs[0].row())          
          self.desc_le.setText(unicode(query.value(2).toString()))        
          if query.value(3).toString().left(1) == "M":
              self.type_cmb.setCurrentIndex(0)
          else:
              self.type_cmb.setCurrentIndex(1)          
          self.usage_le.setText(unicode(query.value(4).toString()))
          self.abonemen_le.setText(unicode(query.value(5).toString()))
          self.ppju_le.setText(unicode(query.value(6).toString()))
          self.loss_le.setText(unicode(query.value(7).toString()))
          self.admin_le.setText(unicode(query.value(8).toString()))
          self.cap_le.setText(unicode(query.value(9).toString()))
          self.other_le.setText(unicode(query.value(10).toString()))
          self.ct_le.setText(unicode(query.value(11).toString()))
          self.range1_le.setText(unicode(query.value(12).toString()))
          self.prog2_le.setText(unicode(query.value(13).toString()))
          self.range2_le.setText(unicode(query.value(14).toString()))
          self.prog3_le.setText(unicode(query.value(15).toString()))
#          self.progYes_rb.setText(unicode(query.value(16).toString()))
          self.trafo_le.setText(unicode(query.value(17).toString()))
          self.maint_le.setText(unicode(query.value(18).toString()))
          self.adminMax_le.setText(unicode(query.value(19).toString()))
          if query.value(20).toString().left(1) == "A":
              self.type_cmb.setCurrentIndex(0)
          else:
              self.type_cmb.setCurrentIndex(1)
          self.note_le.setText(unicode(query.value(21).toString()))
          
  def validasi(self):
      valid = True
      for lineEdit in self.lineedits:
           if lineEdit.text().isEmpty():
               QMessageBox.warning(self, "Tarif Detail","Please cek mandatory column (Yellow background)")
               return False
      return valid

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = TarifDetEdit()
    form.show()
    app.exec_()
